package java_class_work.week08.internet.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author Tiam
 * @Date 2022/11/1 10:30
 * @Description: 服务端-接收数据
 */
public class TcpServer01 {
    public static void main(String[] args) {
        try {
            // 1. 创建Socket, 监听端口
            ServerSocket serverSocket = new ServerSocket(6666);
            // 2. 获取客户端连接
            Socket accept = serverSocket.accept();
            // 3. 获取流资源 接收客户端发送过来的数据
            byte[] bytes = new byte[1024];
            InputStream inputStream = accept.getInputStream();
            // 4. 处理数据
            int len = inputStream.read(bytes);
            System.out.println(new String(bytes,0,len));
            // 5. 关闭流资源和Socket资源
            accept.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
